

package com.jysz.certificate.service.generator;

import org.apache.commons.lang.StringUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.jysz.certificate.persistence.model.system.SequenceNumber;
import com.jysz.certificate.persistence.model.system.SequenceNumber.SequenceType;

/**
 * 默认的代理类.
 * 
 * @version $Revision: 9172 $
 * @since 0.8.0
 */
public class DefaultGeneratorDelegate extends AbstractGeneratorDelegate {
	public static final String DEFAULT_STRING_FORMAT="%1$08d";

	/**
	 * 序号类型
	 */
	SequenceType type;

	/**
	 * 实体类名.
	 */
	String clazzName;
	
	private String format;
	

	public DefaultGeneratorDelegate(SequenceType type, String clazzName) {
		this.type = type;
		this.clazzName = clazzName;
	}
	
	public DefaultGeneratorDelegate(SequenceType type, String clazzName, String format){
		this.type = type;
		this.clazzName = clazzName;
		this.format = format;
	}

	protected String getEntityClassName() {
		return this.clazzName;
	}

	protected SequenceType getSequenceType() {
		return this.type;
	}
	
	public SequenceNumber newSequenceNumber(HibernateTemplate ht) {
		SequenceNumber seq = new SequenceNumber();
		seq.setCurrentNum(0);
		seq.setStepNum(1);
		seq.setEntityClazzName(getEntityClassName());
		seq.setLengthLimited(8);
		if(StringUtils.isNotBlank(this.format)){
			seq.setSeqFormat(this.format);
		} else {
			seq.setSeqFormat(DEFAULT_STRING_FORMAT);
		}
		seq.setSeqType(getSequenceType());
		return seq;
	}

}